using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using VoteMeDefine;
using System.IO;

namespace VoteMe
{
    public partial class FrmStat : Form
    {
        protected string strFiles;
        public FrmStat()
        {
            InitializeComponent();
            initForm();
        }

        private void initForm()
        {
            TxtSQL.Text = @"SELECT PicName,GroupValue,RateValue,VoteBy FROM voteInfo";
            BtnPack.Enabled = false;
            ChkKeep.Enabled = false;

            //throw new Exception("The method or operation is not implemented.");
        }

        private void BtnExcu_Click(object sender, EventArgs e)
        {
            strFiles = "";
            string strSQL = TxtSQL.Text.Trim();
            if ((TxtSQL.Text.Trim().ToLower().IndexOf(@"select") != 0) && (TxtSQL.Text.Trim().ToLower().IndexOf(@"from") > 0))
            {
                MessageBox.Show("Wrong SQL\n\rOnly SELECT is be Support.", "SQL Error");
                TxtSQL.SelectAll();
                TxtSQL.Focus();
                return;
            }
            else
            {

                if (TxtSQL.Text.Trim().ToLower().IndexOf(@"picmd5,") < 0)
                    strSQL=TxtSQL.Text.Trim().Insert(7, @"PicMD5,");

                VariableDefine.meConn.Open();
                SqlCeResultSet meRs;
                SqlCeResultSet meRsMD5;
                try
                {
                    VariableDefine.meCmd.CommandText = TxtSQL.Text.Trim();
                    meRs = VariableDefine.meCmd.ExecuteResultSet(VariableDefine.meRSOptions);
                    DGResult.DataSource = meRs;
                    
                    
                    //find MD5 for each file;
                    meRs.Close();

                    VariableDefine.meCmd.CommandText = strSQL;
                    meRsMD5 = VariableDefine.meCmd.ExecuteResultSet(VariableDefine.meRSOptions);
                    while (meRsMD5.Read())
                    {
                        strFiles = strFiles + meRsMD5["PicMD5"].ToString() + "|";
                    }
                    
                    meRsMD5.Close();
                }
                catch (Exception er)
                {

                    MessageBox.Show(er.ToString(), @"SQL Error");
                    TxtSQL.SelectAll();
                    TxtSQL.Focus();
                }
                finally
                {
                    meRs = null;
                    meRsMD5 = null;
                    VariableDefine.meConn.Close();
                    if (strFiles.Length > 0)
                    {
                        BtnPack.Enabled = true;
                        ChkKeep.Enabled = true;
                    }
                }
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            FrmMain frmMain = new FrmMain();
            frmMain.Show();
            this.Close();
        }

        private void BtnPack_Click(object sender, EventArgs e)
        {
            //find pics from pic path
            //create pack folder
            string strPackFolderFullName=VariableDefine.picturePath +@"\Pack@"+DateTime.Now.ToString(VariableDefine.dateFormate).Replace(':','-');
            if(!Directory.Exists(strPackFolderFullName))
                Directory.CreateDirectory(strPackFolderFullName);
            
            //read md5 from variable
            string[] strFilesMD5 = strFiles.Split('|');

            foreach (string flname in Directory.GetFiles(VariableDefine.picturePath))
            {
                for (int i = 0; i < strFilesMD5.Length - 1; i++)
                {
                    if (strFilesMD5[i] == VariableDefine.getFileMD5(flname))
                    {
                        FileInfo fl;
                        try
                        {

                            //copy or move file
                            fl = new FileInfo(flname);
                            //assembly new file name
                            string[] strTmp = flname.Split('\\');
                            string strShortName = strTmp[strTmp.Length - 1];
                            string newFileFullName = strPackFolderFullName + @"\" + strShortName;
                            if (ChkKeep.Checked)
                            {
                                //copy to
                                fl.CopyTo(newFileFullName);
                            }
                            else
                            {
                                //move to
                                fl.MoveTo(newFileFullName);
                            }
                        }
                        catch
                        {

                        }
                        finally
                        {
                            fl = null;
                        }
                    }
                }
                
            }
            MessageBox.Show(Directory.GetFiles(strPackFolderFullName).Length.ToString() + @"picture(s) exist in " + strPackFolderFullName + @" Folder.", "DONE!");
            FrmMain frmMain=new FrmMain();
            frmMain.Show();
            this.Close();
        }
    }
}